package com.spark.modules.system.entity.params;

import com.spark.common.annotation.valid.Password;
import com.spark.common.constants.RegexConstants;
import com.spark.common.exception.SparkException;
import lombok.Data;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * 系统用户注册实体
 *
 * @author LYCHEE
 * @date 2025/2/18 15:04
 */
@Data
public class RegisterParams implements Serializable {

    /**
     * 用户昵称
     */
    @Size(min = 2, max = 15, message = "昵称长度应在2到15之间")
    private String nickname;

    /**
     * 用户账号
     */
    @Size(min = 5, max = 15, message = "账号长度应在5到15之间")
    @Pattern(regexp = RegexConstants.USERNAME_REGEX, message = "账号只能包含字母和数字")
    private String username;

    /**
     * 用户密码
     */
    @NotBlank(message = "密码不能为空")
    @Password
    private String password;

    /**
     * 确认密码
     */
    @NotBlank(message = "确认密码不能为空")
    @Password
    private String confirmPassword;

    /**
     * 用户邮箱
     */
    @NotBlank(message = "邮箱不能为空")
    @Email(message = "请输入正确的邮箱")
    private String email;

    /**
     * 验证两次密码是否一致
     */
    public void validTwicePassword() {
        if (!this.password.equals(this.confirmPassword)){
            throw new SparkException("两次密码不一致");
        }
    }

}
